DWREngine.setAsync(false);

var mygrid;
var dhxLayout;
var myCalendar1;
var myCalendar2;
//var dhxTabbar;

$(document).ready(function(){
	$( "#refresh" ).button();
	$( "#refresh" ).click(function(){
		refresh();
	});
	$( "#search" ).button();
	$( "#search" ).click(function(){
		search();
	});
	myCalendar1 = new dhtmlXCalendarObject("calendar1");
	myCalendar2 = new dhtmlXCalendarObject("calendar2");
	myCalendar1.setDateFormat("%Y-%m-%d/%H时");
	myCalendar2.setDateFormat("%Y-%m-%d/%H时");
	
//	var dhxLayout;
	dhxLayout = new dhtmlXLayoutObject("parentId", "2U");
	dhxLayout.cont.obj._offsetTop = 53; // top margin
	dhxLayout.cont.obj._offsetHeight = -50; // bottom margin
	dhxLayout.setSizes();
	dhxLayout.cells("a").setText(" ");
	dhxLayout.cells("b").setText(" ");
//	dhxLayout.cells("a").attachObject("acdnmenu");
	dhxLayout.cells("a").setWidth(240);
	dhxLayout.cells("a").fixSize(true, false);

//	dhxTabbar = dhxLayout.cells("b").attachTabbar();
//	dhxTabbar.setImagePath("./dhx/dhtmlxTabbar/imgs/");
//	dhxTabbar.enableTabCloseButton(true);
	mygrid = dhxLayout.cells("b").attachGrid(); // returns grid object
	mygrid.setImagePath("./dhx/dhtmlxGrid/imgs/");//path to images required by grid
	mygrid.setColumnIds("id,cityname,sitename,aqi,monitortime,level,color,type,chiefpollutants,influence,suggest,pm25,o3,pm10,co,so2,no2");//set column names
	mygrid.setHeader("id,城市,站点,aqi,观测时间,级别,颜色,类型,主要污染物,影响,建议,pm25,o3,pm10,co,so2,no2");//set column names
	mygrid.setInitWidths("0,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100");//set column width in px
	mygrid.setColAlign("middle,middle,middle,middle,middle,middle,middle,middle,middle,left,left,middle,middle,middle,middle,middle,middle");//set column values align
	mygrid.setColTypes("ro,ro,ro,ro,ro,ro,ro,ro,ro,ro,ro,ro,ro,ro,ro,ro,ro");//set column types
	mygrid.setColSorting("na,str,str,int,str,str,str,str,str");//set sorting
	mygrid.setStyle("font-size:15px;font-weight:bold;text-align:center", "font-size:15px","", "");
	mygrid.init();//initialize grid
	mygrid.setSkin("dhx_skyblue");//set grid skin
	refresh();
});

function refresh(){
	var date1 = new Date();
	var time1 =	date1.getTime()-date1.getHours()*3600*1000-date1.getMinutes()*60*1000-date1.getSeconds()*1000-date1.getMilliseconds();
	myCalendar1.setDate(new Date(time1));
	myCalendar2.setDate(new Date());
	search();
}

function search(){
	var time1 = myCalendar1.getDate();
	var time2 = myCalendar2.getDate();
	if(time1>=time2){
		$("#info p").html("时间范围错误");
		info();
		return;
	}
	$("#info").hide();
	DwrHzaqi.searchAqiByTime(time1,time2,function(msg){
		mygrid.clearAll();
		mygrid.parse(msg,"js");
		console.log(msg.length);
		$("#info p").html("查询到<span style='color:red'>"+msg.length+"</span>条记录");
		info();
	});
	
	$("tbody tr .ev_dhx_skyblue").attr("style","height:40px;margin-top:5px;margin-bottom:5px");
	$("tbody tr .odd_dhx_skyblue").attr("style","height:40px;margin-top:5px;margin-bottom:5px");
}

function info(){
	$("#info").show();
	$("#info").click(function(){
		$("#info").fadeOut("10000");
	});
} 